<template>
  <div class="container">
    <div class="form-main" ref="fmRef">
      <el-form :inline="true" :model="formMain" ref="formMainRef">
        <el-form-item label="文本" prop="text">
          <el-input v-model="formMain.text" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="选择框" prop="sel">
          <el-select v-model="formMain.sel" placeholder="请选择">
            <el-option label="opt1" value="opt1"></el-option>
            <el-option label="opt2" value="opt2"></el-option>
            <el-option label="opt3" value="opt3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间选择器" prop="datetime">
          <el-date-picker
            v-model="formMain.datetime"
            type="datetime"
            placeholder="Select date and time"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">搜索</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="create">
      <el-button type="primary">新增</el-button>
    </div>
    <div class="table-main" :style="`height: calc(100vh - ${dymHeight}px)`">
      <el-table
        :data="tableData"
        height="100%"
      >
        <el-table-column label="Date" fixed="left" prop="date"></el-table-column>
        <el-table-column label="Name" prop="name"></el-table-column>
        <el-table-column label="State" prop="state"></el-table-column>
        <el-table-column label="City" prop="city"></el-table-column>
        <el-table-column label="Address" prop="address"></el-table-column>
        <el-table-column label="operation" fixed="right">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="openDetail">Detail</el-button>
            <el-button link type="primary" size="small" @click="handleEdit(scope.row)">Edit</el-button>
            <el-button link type="primary" size="small" @click="handleDelete(scope.row)">Delete</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        background
        hide-on-single-page
        layout="prev, pager, next"
        :page-size="pagination.pageSize"
        :total="pagination.total"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, nextTick, onBeforeMount, onMounted } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import axios from '@/http'
import apis from '@/apis'

const fmRef = ref<Element | null>(null)
const dymHeight = ref<number>(254)

const formMainRef = ref<FormInstance | null>(null)
const formMain = reactive({
  text: '',
  sel: '',
  datetime: ''
})
const onSearch = () => {
  console.log('formMain', formMain)
}
const onReset = () => {
  if (!formMainRef.value) return
  formMainRef.value.resetFields()
}

interface TableRow {
  id: string
  date: string
  name: string
  state: string
  city: string
  address: string
}

const tableData = ref([])
const openDetail = () => {}
const handleEdit = (row:TableRow) => {}
const handleDelete = (row:TableRow) => {}
const getTableData = () => {
  const params = {
    pageSize: pagination.pageSize,
    currentPage: pagination.currentPage
  }
  apis.getTestTableData(params).then(res=> {
    console.log(res)
    const { data } = res.data
    tableData.value = data.list
    pagination.total = data.total
  })
}

const pagination = reactive({
  total: 0,
  currentPage: 1,
  pageSize: 10
})
const handleCurrentChange = (page:number) => {
  pagination.currentPage = page
  getTableData()
}

const initData = () => {
  getTableData()
}

onBeforeMount(() => {
  initData()
})

onMounted(() => {
  nextTick(() => {
    dymHeight.value += fmRef.value.clientHeight
    console.log(dymHeight)
  })
})
</script>

<style scoped>

</style>
